/*
	Relates undergraduate GPA to earnings.
	Output:	Table A8
*/
 
local seed "C:\Users\jsock\Dropbox\Research\GD\International"

local dataPath "`seed'/Data"
local inputPath "`seed'/InputData"
local figurePath "`seed'/Replication/Figures"
local tablePath "`seed'/Replication/Tables"
local estimatePath "`seed'/Replication/Estimates"
local tempPath "`seed'/Replication/TempData"

********************************************************
* Read in country gdp to get universitycountry gdp
********************************************************

clear 
set more off
set matsize 10000

* Set path and load data 
insheet using "`dataPath'/Salaries_international_dataset_main.csv", comma
drop v1
 
drop if jobtitle == ""
 
drop metro shortname 
drop city basecurrency country_iso  
drop gender birthyear
drop sectorname iscurrentjobflag 

*--------------------------------
* Thresholds used for sample selection
*--------------------------------

scalar country_premia_thresh = 25
 
scalar selection_thresh = 25
 
scalar school_thresh = 25 

*--------------------------------
* Exclude users that leave more than 10 reviews
*--------------------------------

sort fk_userid yearofsalary dateval salid

by fk_userid : gen obsNum = _n

by fk_userid : gen userReviews = _N

drop if userReviews > 10

drop userReviews

*--------------------------------
* Generate additional variables
*--------------------------------

generate exp = yearsofrelevantexpnumber
generate exp_sqrd = exp ^ 2
generate exp_sqrd_100 = exp_sqrd / 100
drop yearsofrelevantexpnumber

generate logbase = log(basesalary * ppp_xrat)

generate log_gdppw = log(gdppw)

generate degree_major = degree + "-" + grpmajor

drop if ppp_xrat == .

*--------------------------------
* Exclude outliers in base pay
*--------------------------------

scalar scalingThresh = 10

generate realbase = basesalary * ppp_xrat
generate outside_thresh =  (realbase < (1/scalingThresh) * gdppw) | (realbase > scalingThresh * gdppw) 
drop realbase

*------------------------------------------
* Create Ranking Bins
*------------------------------------------

generate rankBin = ""
replace rankBin = "A_01_20" if inrange(world_rank,1,20)
replace rankBin = "B_21_50" if inrange(world_rank,21,50)
replace rankBin = "C_51_100" if inrange(world_rank,51,100)
replace rankBin = "D_101_250" if inrange(world_rank,101,250)
replace rankBin = "E_251_500" if inrange(world_rank,251,500)
replace rankBin = "F_501_1000" if inrange(world_rank,501,1000)
replace rankBin = "G_1001_2000" if inrange(world_rank,1001,2000)
replace rankBin = "H_UNRANKED" if world_rank == . & universitycountry != ""

*------------------------------------------
* Create Major of Study Bins
*------------------------------------------

generate majorStudy = ""
replace majorStudy = "Stem" if grpmajor == "Biological Sciences" | grpmajor == "Engineering" | grpmajor == "Physical Sciences" | grpmajor == "Technology"   
replace majorStudy = "Bus_SocSci" if grpmajor == "Business" | grpmajor == "Social Sciences"
replace majorStudy = "Other" if grpmajor == "Arts and Humanities" | grpmajor == "Communication" | grpmajor == "Education" | grpmajor == "Health Service" | grpmajor == "Social Service" | grpmajor == "Other"

*-------------------------------- 
* Drop Singapore Polytechnics which aren't considered by WHED
*--------------------------------

drop if school == "Nanyang Polytechnic"
drop if school == "Ngee Ann Polytechnic"
drop if school == "Republic Polytechnic"
drop if school == "Singapore Polytechnic"
drop if school == "Temasek Polytechnic"

*--------------------------------
* Add work country name
*--------------------------------

generate country_glassdoor = countryname

merge m:1 country_glassdoor using "C:\Users\jsock\Dropbox\Research\GD\International/Data/Country_gdppw.dta"
drop _merge

rename iso work_country_iso
rename log_gdppw_uc log_gdppw_work_country 

drop country_glassdoor 

*--------------------------------
* Add country of study name
*--------------------------------

generate country_glassdoor = universitycountry

merge m:1 country_glassdoor using "C:\Users\jsock\Dropbox\Research\GD\International/Data/Country_gdppw.dta"
drop _merge

rename iso university_country_iso
rename log_gdppw_uc log_gdppw_university_country 

drop country_glassdoor 

*--------------------------------
* Create clean gpa variable (Scholaro unless otherwise)
*--------------------------------

generate cleanGPA = gpa
replace cleanGPA = . if ~inrange(gpa,2,4.33)

* Convert GPA for India - Mumbai University
replace cleanGPA = 4.33 if inrange(gpa,70,100) & school == "University of Mumbai" 
replace cleanGPA = 4 if inrange(gpa,60,69) & school == "University of Mumbai" 
replace cleanGPA = 3.33 if inrange(gpa,55,59) & school == "University of Mumbai" 
replace cleanGPA = 3 if inrange(gpa,50,54) & school == "University of Mumbai" 
replace cleanGPA = 2.33 if inrange(gpa,45,49) & school == "University of Mumbai" 
replace cleanGPA = 2 if inrange(gpa,40,44) & school == "University of Mumbai" 

* Convert GPA for India - Anna University
replace cleanGPA = 4 if inrange(gpa,81,100) & school == "Anna University" 
replace cleanGPA = 3.33 if inrange(gpa,71,80) & school == "Anna University" 
replace cleanGPA = 3 if inrange(gpa,61,70) & school == "Anna University" 
replace cleanGPA = 2.67 if inrange(gpa,57,60) & school == "Anna University" 
replace cleanGPA = 2 if inrange(gpa,50,56) & school == "Anna University" 

* Convert GPA for India - Bits
replace cleanGPA = 4 if inrange(gpa,9,10) & school == "Birla Institute of Technology and Science" 
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & school == "Birla Institute of Technology and Science" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "Birla Institute of Technology and Science" 
replace cleanGPA = 2.67 if inrange(gpa,6,6.99) & school == "Birla Institute of Technology and Science" 
replace cleanGPA = 2 if inrange(gpa,4,5.99) & school == "Birla Institute of Technology and Science" 

* Convert GPA for India - Cochin University of Science and Technology
replace cleanGPA = 4 if inrange(gpa,9,10) & school == "Cochin University of Science and Technology" 
replace cleanGPA = 3.67 if inrange(gpa,8,8.99) & school == "Cochin University of Science and Technology" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "Cochin University of Science and Technology" 
replace cleanGPA = 2.67 if inrange(gpa,6,6.99) & school == "Cochin University of Science and Technology" 

* Convert GPA for India - Christ University
replace cleanGPA = 4.33 if inrange(gpa,80,100) & school == "Christ University" 
replace cleanGPA = 4 if inrange(gpa,73,79) & school == "Christ University" 
replace cleanGPA = 3.33 if inrange(gpa,66,72) & school == "Christ University" 
replace cleanGPA = 3 if inrange(gpa,60,65) & school == "Christ University" 
replace cleanGPA = 2.67 if inrange(gpa,55,59) & school == "Christ University" 
replace cleanGPA = 2 if inrange(gpa,45,54) & school == "Christ University" 

* Convert GPA for India - Indian Institute of Technology Gandhinagar
replace cleanGPA = 4.33 if inrange(gpa,11,11) & school == "Indian Institute of Technology Gandhinagar" 
replace cleanGPA = 4 if inrange(gpa,9,10.99) & school == "Indian Institute of Technology Gandhinagar" 
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & school == "Indian Institute of Technology Gandhinagar" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "Indian Institute of Technology Gandhinagar" 
replace cleanGPA = 2.67 if inrange(gpa,6,6.99) & school == "Indian Institute of Technology Gandhinagar" 
replace cleanGPA = 2 if inrange(gpa,4,5.99) & school == "Indian Institute of Technology Gandhinagar" 

* Convert GPA for India - Indian Institute of Technology Kanpur
replace cleanGPA = 4 if inrange(gpa,10,11.99) & school == "Indian Institute of Technology Kanpur" 
replace cleanGPA = 3.33 if inrange(gpa,8,9.99) & school == "Indian Institute of Technology Kanpur" 
replace cleanGPA = 2.67 if inrange(gpa,6,7.99) & school == "Indian Institute of Technology Kanpur" 
replace cleanGPA = 2 if inrange(gpa,4,5.99) & school == "Indian Institute of Technology Kanpur" 

* Convert GPA for India - Indian Institute of Technology Roorkee
replace cleanGPA = 4.33 if inrange(gpa,10,10.99) & school == "Indian Institute of Technology Roorkee" 
replace cleanGPA = 4 if inrange(gpa,9,9.99) & school == "Indian Institute of Technology Roorkee" 
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & school == "Indian Institute of Technology Roorkee" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "Indian Institute of Technology Roorkee" 
replace cleanGPA = 2.67 if inrange(gpa,6,6.99) & school == "Indian Institute of Technology Roorkee" 
replace cleanGPA = 2 if inrange(gpa,4,5.99) & school == "Indian Institute of Technology Roorkee" 

* Convert GPA for India - Integral University
replace cleanGPA = 4 if inrange(gpa,80,100) & school == "Integral University" 
replace cleanGPA = 3.33 if inrange(gpa,75,79.99) & school == "Integral University" 
replace cleanGPA = 3 if inrange(gpa,65,74.99) & school == "Integral University" 
replace cleanGPA = 2.67 if inrange(gpa,60,64.99) & school == "Integral University" 
replace cleanGPA = 2 if inrange(gpa,41,59.99) & school == "Integral University" 

* Convert GPA for India - Jain University
replace cleanGPA = 4.33 if inrange(gpa,95,100) & school == "Jain University" 
replace cleanGPA = 4 if inrange(gpa,85,94.99) & school == "Jain University" 
replace cleanGPA = 3.67 if inrange(gpa,80,84.99) & school == "Jain University" 
replace cleanGPA = 3.33 if inrange(gpa,75,79.99) & school == "Jain University" 
replace cleanGPA = 3 if inrange(gpa,60,74.99) & school == "Jain University" 
replace cleanGPA = 2.67 if inrange(gpa,55,59.99) & school == "Jain University" 
replace cleanGPA = 2.33 if inrange(gpa,50,54.99) & school == "Jain University" 
replace cleanGPA = 2 if inrange(gpa,40,49.99) & school == "Jain University" 

* Convert GPA for India - Dr. A.P.J. Abdul Kalam Technical University
replace cleanGPA = 4 if inrange(gpa,85,100) & school == "Dr. A.P.J. Abdul Kalam Technical University" 
replace cleanGPA = 3.33 if inrange(gpa,70,84.99) & school == "Dr. A.P.J. Abdul Kalam Technical University" 
replace cleanGPA = 3 if inrange(gpa,60,69.99) & school == "Dr. A.P.J. Abdul Kalam Technical University" 
replace cleanGPA = 2.67 if inrange(gpa,50,59.99) & school == "Dr. A.P.J. Abdul Kalam Technical University" 
replace cleanGPA = 2 if inrange(gpa,45,49.99) & school == "Dr. A.P.J. Abdul Kalam Technical University" 

* Convert GPA for India - Mahatma Gandhi University
replace cleanGPA = 4 if inrange(gpa,4,4.99) & school == "Mahatma Gandhi University" 
replace cleanGPA = 3.33 if inrange(gpa,3,3.99) & school == "Mahatma Gandhi University" 
replace cleanGPA = 3 if inrange(gpa,2,2.99) & school == "Mahatma Gandhi University" 
replace cleanGPA = 2 if inrange(gpa,1,1.99) & school == "Mahatma Gandhi University" 

* Convert GPA for India - Manipal Institute of Technology
replace cleanGPA = 4 if inrange(gpa,9,10.99) & school == "Manipal Institute of Technology" 
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & school == "Manipal Institute of Technology" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "Manipal Institute of Technology" 
replace cleanGPA = 2.67 if inrange(gpa,6,6.99) & school == "Manipal Institute of Technology" 
replace cleanGPA = 2 if inrange(gpa,5,5.99) & school == "Manipal Institute of Technology" 

* Convert GPA for India - Nirma University
replace cleanGPA = 4 if inrange(gpa,9,10.99) & school == "Nirma University" 
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & school == "Nirma University" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "Nirma University" 
replace cleanGPA = 2.33 if inrange(gpa,6,6.99) & school == "Nirma University" 
replace cleanGPA = 2 if inrange(gpa,5,5.99) & school == "Nirma University" 

* Convert GPA for India - Shiv Nadar University
replace cleanGPA = 4 if inrange(gpa,9,10.99) & school == "Shiv Nadar University" 
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & school == "Shiv Nadar University" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "Shiv Nadar University" 
replace cleanGPA = 2.67 if inrange(gpa,6,6.99) & school == "Shiv Nadar University" 
replace cleanGPA = 2 if inrange(gpa,4,5.99) & school == "Shiv Nadar University" 

* Convert GPA for India - SASTRA University
replace cleanGPA = 4 if inrange(gpa,9,10.99) & school == "SASTRA University" 
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & school == "SASTRA University" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "SASTRA University" 
replace cleanGPA = 2.67 if inrange(gpa,6,6.99) & school == "SASTRA University" 
replace cleanGPA = 2 if inrange(gpa,4,5.99) & school == "SASTRA University" 

* Convert GPA for India - Saurashtra University
replace cleanGPA = 4 if inrange(gpa,80,100) & school == "Saurashtra University" 
replace cleanGPA = 3.33 if inrange(gpa,70,79.99) & school == "Saurashtra University" 
replace cleanGPA = 3 if inrange(gpa,60,69.99) & school == "Saurashtra University" 
replace cleanGPA = 2.67 if inrange(gpa,50,59.99) & school == "Saurashtra University" 
replace cleanGPA = 2 if inrange(gpa,40,49.99) & school == "Saurashtra University" 

* Convert GPA for India - SRM Institute of Science and Technology
replace cleanGPA = 4 if inrange(gpa,9,10.99) & school == "SRM Institute of Science and Technology" 
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & school == "SRM Institute of Science and Technology" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "SRM Institute of Science and Technology" 
replace cleanGPA = 2.67 if inrange(gpa,6,6.99) & school == "SRM Institute of Science and Technology" 
replace cleanGPA = 2 if inrange(gpa,4,5.99) & school == "SRM Institute of Science and Technology" 

* Convert GPA for India - Saurashtra University
replace cleanGPA = 4 if inrange(gpa,90,100) & school == "University of Madras" 
replace cleanGPA = 3.33 if inrange(gpa,80,89.99) & school == "University of Madras" 
replace cleanGPA = 3 if inrange(gpa,70,79.99) & school == "University of Madras" 
replace cleanGPA = 2.67 if inrange(gpa,60,69.99) & school == "University of Madras" 
replace cleanGPA = 2 if inrange(gpa,40,59.99) & school == "University of Madras" 

* Convert GPA for India - University of Petroleum and Energy Studies
replace cleanGPA = 4 if inrange(gpa,81,100) & school == "University of Petroleum and Energy Studies" 
replace cleanGPA = 3.33 if inrange(gpa,71,80) & school == "University of Petroleum and Energy Studies" 
replace cleanGPA = 3 if inrange(gpa,61,70) & school == "University of Petroleum and Energy Studies" 
replace cleanGPA = 2.67 if inrange(gpa,51,60) & school == "University of Petroleum and Energy Studies" 
replace cleanGPA = 2 if inrange(gpa,40,50) & school == "University of Petroleum and Energy Studies" 

* Convert GPA for India - Vellore Institute of Technology
replace cleanGPA = 4 if inrange(gpa,9,10.99) & school == "Vellore Institute of Technology" 
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & school == "Vellore Institute of Technology" 
replace cleanGPA = 3 if inrange(gpa,7,7.99) & school == "Vellore Institute of Technology" 
replace cleanGPA = 2.67 if inrange(gpa,6,6.99) & school == "Vellore Institute of Technology" 
replace cleanGPA = 2 if inrange(gpa,5,5.99) & school == "Vellore Institute of Technology" 

* Convert GPA for India
replace cleanGPA = 4 if inrange(gpa,60,100) & universitycountry == "India" & school != "University of Mumbai" & school != "Anna University" & school != "Birla Institute of Technology and Science" & school != "Cochin University of Science and Technology" & school != "Christ University" & school != "Indian Institute of Technology Gandhinagar" & school != "Indian Institute of Technology Kanpur" & school != "Indian Institute of Technology Roorkee" & school != "Integral University" & school != "Jain University" & school != "Dr. A.P.J. Abdul Kalam Technical University" & school != "Mahatma Gandhi University" & school != "Manipal Institute of Technology" & school != "Nirma University" & school != "Shiv Nadar University" & school != "SASTRA University" & school != "Saurashtra University" & school != "SRM Institute of Science and Technology" & school != "University of Madras" & school != "University of Petroleum and Energy Studies" & school != "Vellore Institute of Technology"
replace cleanGPA = 3.5 if inrange(gpa,55,59) & universitycountry == "India" & school != "University of Mumbai" & school != "Anna University" & school != "Birla Institute of Technology and Science" & school != "Cochin University of Science and Technology" & school != "Christ University" & school != "Indian Institute of Technology Gandhinagar" & school != "Indian Institute of Technology Kanpur" & school != "Indian Institute of Technology Roorkee" & school != "Integral University" & school != "Jain University" & school != "Dr. A.P.J. Abdul Kalam Technical University" & school != "Mahatma Gandhi University" & school != "Manipal Institute of Technology" & school != "Nirma University" & school != "Shiv Nadar University" & school != "SASTRA University" & school != "Saurashtra University" & school != "SRM Institute of Science and Technology" & school != "University of Madras" & school != "University of Petroleum and Energy Studies" & school != "Vellore Institute of Technology"
replace cleanGPA = 3 if inrange(gpa,50,54) & universitycountry == "India" & school != "University of Mumbai" & school != "Anna University" & school != "Birla Institute of Technology and Science" & school != "Cochin University of Science and Technology" & school != "Christ University" & school != "Indian Institute of Technology Gandhinagar" & school != "Indian Institute of Technology Kanpur" & school != "Indian Institute of Technology Roorkee" & school != "Integral University" & school != "Jain University" & school != "Dr. A.P.J. Abdul Kalam Technical University" & school != "Mahatma Gandhi University" & school != "Manipal Institute of Technology" & school != "Nirma University" & school != "Shiv Nadar University" & school != "SASTRA University" & school != "Saurashtra University" & school != "SRM Institute of Science and Technology" & school != "University of Madras" & school != "University of Petroleum and Energy Studies" & school != "Vellore Institute of Technology"
replace cleanGPA = 2.5 if inrange(gpa,43,49) & universitycountry == "India" & school != "University of Mumbai" & school != "Anna University" & school != "Birla Institute of Technology and Science" & school != "Cochin University of Science and Technology" & school != "Christ University" & school != "Indian Institute of Technology Gandhinagar" & school != "Indian Institute of Technology Kanpur" & school != "Indian Institute of Technology Roorkee" & school != "Integral University" & school != "Jain University" & school != "Dr. A.P.J. Abdul Kalam Technical University" & school != "Mahatma Gandhi University" & school != "Manipal Institute of Technology" & school != "Nirma University" & school != "Shiv Nadar University" & school != "SASTRA University" & school != "Saurashtra University" & school != "SRM Institute of Science and Technology" & school != "University of Madras" & school != "University of Petroleum and Energy Studies" & school != "Vellore Institute of Technology"
replace cleanGPA = 2 if inrange(gpa,35,42) & universitycountry == "India" & school != "University of Mumbai" & school != "Anna University" & school != "Birla Institute of Technology and Science" & school != "Cochin University of Science and Technology" & school != "Christ University" & school != "Indian Institute of Technology Gandhinagar" & school != "Indian Institute of Technology Kanpur" & school != "Indian Institute of Technology Roorkee" & school != "Integral University" & school != "Jain University" & school != "Dr. A.P.J. Abdul Kalam Technical University" & school != "Mahatma Gandhi University" & school != "Manipal Institute of Technology" & school != "Nirma University" & school != "Shiv Nadar University" & school != "SASTRA University" & school != "Saurashtra University" & school != "SRM Institute of Science and Technology" & school != "University of Madras" & school != "University of Petroleum and Energy Studies" & school != "Vellore Institute of Technology"

* Convert GPA for Australia (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,83,100) & universitycountry == "Australia"
replace cleanGPA = 3.33 if inrange(gpa,73,82.99) & universitycountry == "Australia"
replace cleanGPA = 3 if inrange(gpa,63,72.99) & universitycountry == "Australia"
replace cleanGPA = 2.33 if inrange(gpa,50,62.99) & universitycountry == "Australia"

* Convert GPA for Canada (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,80,100) & universitycountry == "Canada"
replace cleanGPA = 3.33 if inrange(gpa,77,79.99) & universitycountry == "Canada"
replace cleanGPA = 3 if inrange(gpa,73,76.99) & universitycountry == "Canada"
replace cleanGPA = 2.33 if inrange(gpa,70,72.99) & universitycountry == "Canada"

* Convert GPA for Denmark (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,12,12) & universitycountry == "Denmark"
replace cleanGPA = 3.33 if inrange(gpa,10,11.99) & universitycountry == "Denmark"
replace cleanGPA = 3 if inrange(gpa,7,9.99) & universitycountry == "Denmark"
replace cleanGPA = 2.33 if inrange(gpa,4,6.99) & universitycountry == "Denmark"

* Convert GPA for France (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,15,20) & universitycountry == "France"
replace cleanGPA = 3.33 if inrange(gpa,13,14.99) & universitycountry == "France"
replace cleanGPA = 3 if inrange(gpa,11,12.99) & universitycountry == "France"
replace cleanGPA = 2.33 if inrange(gpa,9,10.99) & universitycountry == "France"

* Convert GPA for Germany (YAP guidelines)
*replace cleanGPA = 4 if inrange(gpa,1,1.5) & universitycountry == "Germany"
*replace cleanGPA = 3.33 if inrange(gpa,1.51,2.5) & universitycountry == "Germany"
*replace cleanGPA = 3 if inrange(gpa,2.51,3.5) & universitycountry == "Germany"
*replace cleanGPA = 2.33 if inrange(gpa,3.51,4) & universitycountry == "Germany"

* Convert GPA for Greece (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,8.5,10) & universitycountry == "Greece"
replace cleanGPA = 3.33 if inrange(gpa,6.5,8.49) & universitycountry == "Greece"
replace cleanGPA = 3 if inrange(gpa,5,6.49) & universitycountry == "Greece"
*replace cleanGPA = 2.33 if inrange(gpa,0,4.99) & universitycountry == "Greece"

* Convert GPA for Hungary (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,86,100) & universitycountry == "Hungary"
replace cleanGPA = 3.33 if inrange(gpa,71,85.99) & universitycountry == "Hungary"
replace cleanGPA = 3 if inrange(gpa,61,70.99) & universitycountry == "Hungary"
replace cleanGPA = 2.33 if inrange(gpa,51,60.99) & universitycountry == "Hungary"
replace cleanGPA = 4 if inrange(gpa,5,5) & universitycountry == "Hungary"
replace cleanGPA = 3.33 if inrange(gpa,4,4.99) & universitycountry == "Hungary"
replace cleanGPA = 3 if inrange(gpa,3,3.99) & universitycountry == "Hungary"
replace cleanGPA = 2.33 if inrange(gpa,2,2.99) & universitycountry == "Hungary"

* Convert GPA for Greece (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,8.5,10) & universitycountry == "Greece"
replace cleanGPA = 3.33 if inrange(gpa,6.5,8.49) & universitycountry == "Greece"
replace cleanGPA = 3 if inrange(gpa,5,6.49) & universitycountry == "Greece"
*replace cleanGPA = 2.33 if inrange(gpa,0,4.99) & universitycountry == "Greece"

* Convert GPA for Ireland (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,70,100) & universitycountry == "Ireland"
replace cleanGPA = 3.33 if inrange(gpa,66,69.99) & universitycountry == "Ireland"
replace cleanGPA = 3 if inrange(gpa,60,65.99) & universitycountry == "Ireland"
replace cleanGPA = 2.33 if inrange(gpa,50,59.99) & universitycountry == "Ireland"

* Convert GPA for Israel (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,95,100) & universitycountry == "Israel"
replace cleanGPA = 3.33 if inrange(gpa,85,94.99) & universitycountry == "Israel"
replace cleanGPA = 3 if inrange(gpa,75,84.99) & universitycountry == "Israel"
replace cleanGPA = 2.33 if inrange(gpa,65,74.99) & universitycountry == "Israel"
replace cleanGPA = 4 if inrange(gpa,10,10) & universitycountry == "Israel"
replace cleanGPA = 3.33 if inrange(gpa,9,9.99) & universitycountry == "Israel"
replace cleanGPA = 3 if inrange(gpa,8,8.99) & universitycountry == "Israel"
replace cleanGPA = 2.33 if inrange(gpa,7,7.99) & universitycountry == "Israel"

* Convert GPA for Italy (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,29,30) & universitycountry == "Italy"
replace cleanGPA = 3.33 if inrange(gpa,27,28.99) & universitycountry == "Italy"
replace cleanGPA = 3 if inrange(gpa,24,26.99) & universitycountry == "Italy"
replace cleanGPA = 2.33 if inrange(gpa,19,23.99) & universitycountry == "Italy"

* Convert GPA for Japan (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,90,100) & universitycountry == "Japan"
replace cleanGPA = 3.33 if inrange(gpa,80,89.99) & universitycountry == "Japan"
replace cleanGPA = 3 if inrange(gpa,70,79.99) & universitycountry == "Japan"
replace cleanGPA = 2.33 if inrange(gpa,60,69.99) & universitycountry == "Japan"

* Convert GPA for Mexico (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,90,100) & universitycountry == "Mexico"
replace cleanGPA = 3.33 if inrange(gpa,80,89.99) & universitycountry == "Mexico"
replace cleanGPA = 3 if inrange(gpa,60,79.99) & universitycountry == "Mexico"
replace cleanGPA = 2.33 if inrange(gpa,0,59.99) & universitycountry == "Mexico"
replace cleanGPA = 4 if inrange(gpa,9,10) & universitycountry == "Mexico"
replace cleanGPA = 3.33 if inrange(gpa,8,8.99) & universitycountry == "Mexico"
replace cleanGPA = 3 if inrange(gpa,6,7.99) & universitycountry == "Mexico"
replace cleanGPA = 2.33 if inrange(gpa,4.33,5.99) & universitycountry == "Mexico"

* Convert GPA for Netherlands (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,8.5,10) & universitycountry == "Netherlands"
replace cleanGPA = 3.33 if inrange(gpa,7.5,8.49) & universitycountry == "Netherlands"
replace cleanGPA = 3 if inrange(gpa,6.5,7.49) & universitycountry == "Netherlands"
replace cleanGPA = 2.33 if inrange(gpa,6,6.49) & universitycountry == "Netherlands"

* Convert GPA for Portugal (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,18,20) & universitycountry == "Portugal"
replace cleanGPA = 3.33 if inrange(gpa,16,17.99) & universitycountry == "Portugal"
replace cleanGPA = 3 if inrange(gpa,14,15.99) & universitycountry == "Portugal"
replace cleanGPA = 2.33 if inrange(gpa,10,13.99) & universitycountry == "Portugal"

* Convert GPA for Spain (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,9,10) & universitycountry == "Spain"
replace cleanGPA = 3.33 if inrange(gpa,7,8.99) & universitycountry == "Spain"
replace cleanGPA = 3 if inrange(gpa,6,6.99) & universitycountry == "Spain"
replace cleanGPA = 2.33 if inrange(gpa,5.5,5.99) & universitycountry == "Spain"

* Convert GPA for Turkey (YAP guidelines)
replace cleanGPA = 4 if inrange(gpa,90,100) & universitycountry == "Turkey"
replace cleanGPA = 3.33 if inrange(gpa,85,89) & universitycountry == "Turkey"
replace cleanGPA = 3 if inrange(gpa,80,84) & universitycountry == "Turkey"
replace cleanGPA = 2.33 if inrange(gpa,75,79) & universitycountry == "Turkey"

* Convert GPA for Bangladesh (Scholaro)
replace cleanGPA = 4 if inrange(gpa,60,100) & universitycountry == "Bangladesh"
replace cleanGPA = 3.33 if inrange(gpa,55,59.99) & universitycountry == "Bangladesh"
replace cleanGPA = 3 if inrange(gpa,50,54.99) & universitycountry == "Bangladesh"
replace cleanGPA = 2.33 if inrange(gpa,43,49.99) & universitycountry == "Bangladesh"
replace cleanGPA = 2 if inrange(gpa,35,42.99) & universitycountry == "Bangladesh"

* Convert GPA for United Kingdom
*http://www.fulbright.org.uk/going-to-the-usa/pre-departure/academics
replace cleanGPA = 4 if inrange(gpa,70,100) & universitycountry == "United Kingdom"
replace cleanGPA = 3.7 if inrange(gpa,65,69) & universitycountry == "United Kingdom"
replace cleanGPA = 3.3 if inrange(gpa,60,64) & universitycountry == "United Kingdom"
replace cleanGPA = 3.0 if inrange(gpa,55,59) & universitycountry == "United Kingdom"
replace cleanGPA = 2.7 if inrange(gpa,50,54) & universitycountry == "United Kingdom"
replace cleanGPA = 2.3 if inrange(gpa,45,49) & universitycountry == "United Kingdom"
replace cleanGPA = 2.0 if inrange(gpa,40,44) & universitycountry == "United Kingdom"

generate logGPA = log(cleanGPA)

bys universitycountry school : egen z_gpa = std(cleanGPA)

*--------------------------------
* Generate indicators for wage in same country as university or not
*--------------------------------

generate home_country = countryname == universitycountry & universitycountry != ""

generate foreign_country = countryname != universitycountry & universitycountry != ""

*--------------------------------
* Generate indicators for valid in education analysis
*--------------------------------

* For first degree

generate hasDegree = degree != "UNMATCHED" & degree != "missing" & degree != ""  & degree != "HIGHSCHOOL"

generate uniDegree = degree == "BACHELORS" 

generate hasSchool = school != ""

* For second degree

generate hasDegree_2 = degree_2 != "UNMATCHED" & degree_2 != "missing" & degree_2 != ""  & degree != "HIGHSCHOOL"

generate uniDegree_2 = degree_2 != "ASSOCIATES" & degree_2 != "DIPLOMA"  & degree_2 != "HIGHSCHOOL" & degree_2 != "BACHELORS"

generate hasSchool_2 = school_2 != ""

replace degree_2 = "NONE" if degree_2 == ""

generate gradSchool = hasDegree_2 & uniDegree_2

*--------------------------------
* Drop unused obs
*--------------------------------

drop if employertypecode == "SELF_EMPLOYED" 

drop if outside_thresh

generate valid_educ = uniDegree & hasDegree & hasSchool & universitycountry != ""
keep if valid_educ

********************************************************
* SAMPLE SIZE FOR COUNTRY PREMIA Z_C (MOVERS)
********************************************************

*--------------------------------
* Determine top 10 destinations
*--------------------------------

sort fk_userid yearofsalary dateval salid

by fk_userid: generate destination = countryname[_n+1]

generate migrant = destination != countryname & destination != ""

bys destination : egen destinationMigrants = sum(migrant)
bys destination : replace destinationMigrants = . if destination == ""
bys destination : replace destinationMigrants = . if _n > 1

replace destinationMigrants = destinationMigrants * -1

sort destinationMigrants 

* Determine top destination (Was 2.5% of migrants, now 1.0%)

egen totalMigrants = sum(destinationMigrants * -1)
	
generate migrant_share = -1 * destinationMigrants / totalMigrants
	
generate top_destination = migrant_share >= 0.01 & migrant_share != .

bys destination : egen topDestination = max(top_destination)

*--------------------------------
* Determine # of migrants to top ten destination for each origin
*--------------------------------

bys countryname : egen migrantsOverall = sum(migrant)  

bys countryname : egen migrantsTop = sum(migrant * topDestination)  

generate valid_country_premia = migrantsTop >= country_premia_thresh & migrantsTop != .

drop destinationMigrants top_destination totalMigrants 

********************************************************
* STEP 0: SETUP FOR SKILL LOSS 
********************************************************

sort fk_userid yearofsalary dateval salid
 
by fk_userid: gen prev_country = countryname[_n-1]

*--------------------------------
* Determine if migrated
*--------------------------------

generate migrated = countryname != prev_country & prev_country != ""

sort fk_userid yearofsalary dateval salid
by fk_userid: gen already_migrated = sum(migrated)

*--------------------------------
* Generate variable capturing delta_gdppw for migrants - between countries of work
*--------------------------------

sort fk_userid yearofsalary dateval salid
by fk_userid: gen prev_gdp = log_gdppw_work_country[_n-1]
generate delta_log_gdppw = abs(log_gdppw_work_country - prev_gdp)

generate migrant_delta_gdppw_work = 0
by fk_userid: replace migrant_delta_gdppw_work = delta_log_gdppw if migrated == 1 
by fk_userid: replace migrant_delta_gdppw_work = migrant_delta_gdppw_work[_n-1] if migrated == 0 & migrated[_n-1] == 1
by fk_userid: replace migrant_delta_gdppw_work = migrant_delta_gdppw_work[_n-2] if migrated == 0 & migrated[_n-1] == 0 & migrated[_n-2] == 1
by fk_userid: replace migrant_delta_gdppw_work = migrant_delta_gdppw_work[_n-3] if migrated == 0 & migrated[_n-1] == 0 & migrated[_n-2] == 0 & migrated[_n-3] == 1
by fk_userid: replace migrant_delta_gdppw_work = migrant_delta_gdppw_work[_n-4] if migrated == 0 & migrated[_n-1] == 0 & migrated[_n-2] == 0 & migrated[_n-3] == 0 & migrated[_n-4] == 1
by fk_userid: replace migrant_delta_gdppw_work = migrant_delta_gdppw_work[_n-5] if migrated == 0 & migrated[_n-1] == 0 & migrated[_n-2] == 0 & migrated[_n-3] == 0 & migrated[_n-4] == 0 & migrated[_n-5] == 1
by fk_userid: replace migrant_delta_gdppw_work = migrant_delta_gdppw_work[_n-6] if migrated == 0 & migrated[_n-1] == 0 & migrated[_n-2] == 0 & migrated[_n-3] == 0 & migrated[_n-4] == 0 & migrated[_n-5] == 0 & migrated[_n-6] == 1
by fk_userid: replace migrant_delta_gdppw_work = migrant_delta_gdppw_work[_n-7] if migrated == 0 & migrated[_n-1] == 0 & migrated[_n-2] == 0 & migrated[_n-3] == 0 & migrated[_n-4] == 0 & migrated[_n-5] == 0 & migrated[_n-6] == 0 & migrated[_n-7] == 1
by fk_userid: replace migrant_delta_gdppw_work = migrant_delta_gdppw_work[_n-8] if migrated == 0 & migrated[_n-1] == 0 & migrated[_n-2] == 0 & migrated[_n-3] == 0 & migrated[_n-4] == 0 & migrated[_n-5] == 0 & migrated[_n-6] == 0 & migrated[_n-7] == 0 & migrated[_n-8] == 1

*--------------------------------
* Generate coarse desination FE for migration 
*	Keep top destinations alone, group others by continent
*--------------------------------

	* Top destinations
	
	preserve

		keep destination topDestination
		rename destination countryname
		rename topDestination is_topDestination
		
		bys countryname : keep if _n == 1
		
		tempfile in_results
		save `in_results', emptyok
		
	restore

	merge m:1 countryname using `in_results'
	drop if _merge == 2
	drop _merge

	generate coarseDestination = ""
	replace coarseDestination = countryname if is_topDestination == 1
	drop is_topDestination

	* Continents
	
	preserve

		clear
		
		insheet using "C:\Users\jsock\Dropbox\Research\GD\International\InputData\Country_continents.csv" , comma
		
		rename v2 countryname
		rename v7 continent
		keep countryname continent
		
		bys countryname : keep if _n == 1
		
		replace countryname = "Czech Republic" if countryname == "Czechia"
		replace countryname = "Iran" if countryname == "Iran (Islamic Republic of)"
		replace countryname = "Russia" if countryname == "Russian Federation"
		replace countryname = "Hong Kong" if countryname == "China, Hong Kong Special Administrative Region"
		replace countryname = "South Korea" if countryname == "Republic of Korea"
		replace countryname = "United Kingdom" if countryname == "United Kingdom of Great Britain and Northern Ireland"
		replace countryname = "United States" if countryname == "United States of America"
		replace countryname = "Vietnam" if countryname == "Viet Nam"
		
		tempfile in_results
		save `in_results', emptyok
		
	restore

	merge m:1 countryname using `in_results'
	drop if _merge == 2
	drop _merge

	replace coarseDestination = continent if coarseDestination == "" & valid_country_premia
	
	* Destination FE
	
	tab coarseDestination if valid_country_premia == 1, gen(c_)
	foreach my_var of varlist c_*{
		replace `my_var' = 0 if ~(migrated | already_migrated >= 1)
	}	

********************************************************
* ESTIMATE COUNTRY FE : Z_C
********************************************************

bys countryname : gen new_country = _n == 1

reghdfe logbase exp exp_sqrd c_* migrant_delta_gdppw_work if valid_country_premia , absorb(fk_userid fe_country=countryname yearofsalary ) vce(cluster countryname)

bys countryname : egen obs =sum(e(sample))

bys countryname : egen z_c = max(fe_country)

*--------------------------------
* GENERATE tau FOR WORKERS
*--------------------------------

generate tau = 0
foreach my_var of varlist c_* {
	
	bys coarseDestination : egen fill_`my_var' = max(`my_var')
	replace tau = tau + `my_var' * _b["`my_var'"] 
	replace tau = tau + fill_`my_var' * _b["`my_var'"] if `my_var' == 0 & universitycountry != countryname 
	drop fill_*
	
}
foreach my_var of varlist migrant_delta_gdppw_work {

	replace tau = tau + `my_var' * _b["`my_var'"]
	replace tau = tau + abs(log_gdppw_work_country - log_gdppw_university_country) * _b["`my_var'"] if `my_var' == 0 & universitycountry != countryname 

}

generate logbase_minus_z_c = logbase - z_c - tau

generate hasCountryPremia = z_c != .

keep if valid_educ & hasCountryPremia

********************************************************
* Returns to gpa
********************************************************

eststo clear

eststo: reghdfe logbase_minus_z_c z_gpa exp exp_sqrd_100 if universitycountry == "United States" & countryname == "United States" & grpmajor != "Other" & grpmajor != "missing", absorb(school grpmajor yearofsalary) vce(cluster school)

eststo: reghdfe logbase_minus_z_c z_gpa exp exp_sqrd_100 if universitycountry == "United States" & countryname != "United States" & grpmajor != "Other" & grpmajor != "missing", absorb(school grpmajor yearofsalary) vce(cluster school)

eststo: reghdfe logbase_minus_z_c z_gpa exp exp_sqrd_100 if universitycountry != "United States" & countryname == "United States" & grpmajor != "Other" & grpmajor != "missing", absorb(school grpmajor yearofsalary) vce(cluster school)

eststo: reghdfe logbase_minus_z_c z_gpa exp exp_sqrd_100 if universitycountry != "United States" & countryname != "United States" & grpmajor != "Other" & grpmajor != "missing", absorb(school grpmajor yearofsalary) vce(cluster school)

label var z_gpa "Standardized z-score for GPA"
label var exp "Years of experience"
label var exp_sqrd_100 "Years of experience\string^2 / 100" 

* Store information for plotting
local texFile = "`tablePath'/Table_B8.tex"

#delimit ; 
esttab using "`texFile'", b(%6.3f) se(%5.3f) label compress replace obslast depvars nocons nomtitles
	legend star(* 0.10 ** 0.05 *** 0.01) eqlabels(none) nonum
	stats(N r2_a, 
					fmt(%9.0g %3.2f) 
					labels("N" 
						   "Adjusted R$^2$")) 
	drop(_cons )
	prehead(
		\begin{tabular}{l*{@M}{c}} \hline \hline \noalign{\smallskip}
				& \multicolumn{2}{c}{U.S. Colleges} 
				& \multicolumn{2}{c}{Non-U.S. Colleges} \\ 
					\cmidrule(l){2-3} \cmidrule(l){4-5}
				& \multicolumn{1}{c}{U.S. Worker} 
				& \multicolumn{1}{c}{Non-U.S. Worker} 
				& \multicolumn{1}{c}{U.S. Worker} 
				& \multicolumn{1}{c}{Non-U.S. Worker} \\ 
	)
	posthead(\hline \noalign{\smallskip})
	prefoot(\hline \noalign{\smallskip} )
	postfoot(
		\hline \hline \noalign{\smallskip}
		\end{tabular}
	)
; 
#delimit cr
